package cn.com.suiyi.shixingshengxian.commoditydetail;

import android.util.Log;

import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import cn.com.suiyi.shixingshengxian.commoditydetail.bean.Commodity;
import cn.com.suiyi.shixingshengxian.commoditydetail.bean.CommodityPic;
import cn.com.suiyi.shixingshengxian.url.Url;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by hansen on 2016/7/22.
 */
public class CommodityModel implements CommodityContract.ComModel{

    @Override
    public void getCommodity(String path) {
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder().url(Url.BASE_URL+Url.COMMODITY_TOP+path+Url.COMMODITY_BOTTOM).build();
        Log.i("tag",Url.COMMODITY_TOP+path+Url.COMMODITY_BOTTOM);
        Call call=client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Commodity commodity= JSON.parseObject(response.body().string(),Commodity.class);
                EventBus.getDefault().post(commodity);
            }
        });
    }

    @Override
    public void getCommodityPic(String str) {
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder().url(Url.BASE_URL+Url.COMMODITYPIC_TOP+str+Url.COMMODITYPIC_BOTTOM).build();

        Call call=client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                CommodityPic commodityPic= JSON.parseObject(response.body().string(),CommodityPic.class);
                EventBus.getDefault().post(commodityPic);
            }
        });
    }
}
